package com.jhs.boot.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhs.boot.system.converter.SysNodeRouteConverter;
import com.jhs.boot.system.mapper.SysNodeRouteMapper;
import com.jhs.boot.system.model.entity.SysNodeRoute;
import com.jhs.boot.system.model.form.SysNodeRouteForm;
import com.jhs.boot.system.model.query.SysNodeRouteQuery;
import com.jhs.boot.system.model.vo.SysNodeRouteVO;
import com.jhs.boot.system.service.SysNodeRouteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 节点和路由关联服务实现类
 *
 * @author zxy
 * @since 2025-11-22 11:28
 */
@Service
@RequiredArgsConstructor
public class SysNodeRouteServiceImpl extends ServiceImpl<SysNodeRouteMapper, SysNodeRoute> implements SysNodeRouteService {
    private final SysNodeRouteConverter sysNodeRouteConverter;
    /**
    * 获取节点和路由关联分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysNodeRouteVO>} 节点和路由关联分页列表
    */
    @Override
    public IPage<SysNodeRouteVO> getSysNodeRoutePage(SysNodeRouteQuery queryParams) {
        Page<SysNodeRouteVO> pageVO = this.baseMapper.getSysNodeRoutePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取节点和路由关联表单数据
     *
     * @param id 节点和路由关联ID
     * @return 节点和路由关联表单数据
     */
    @Override
    public SysNodeRouteForm getSysNodeRouteFormData(Long id) {
        SysNodeRoute entity = this.getById(id);
        return sysNodeRouteConverter.toForm(entity);
    }
    
    /**
     * 新增节点和路由关联
     *
     * @param formData 节点和路由关联表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSysNodeRoute(SysNodeRouteForm formData) {
        SysNodeRoute entity = sysNodeRouteConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新节点和路由关联
     *
     * @param id   节点和路由关联ID
     * @param formData 节点和路由关联表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSysNodeRoute(Long id,SysNodeRouteForm formData) {
        SysNodeRoute entity = sysNodeRouteConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除节点和路由关联
     *
     * @param ids 节点和路由关联ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSysNodeRoutes(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的节点和路由关联数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();


        return this.remove(new LambdaQueryWrapper<SysNodeRoute>().in(SysNodeRoute::getRouteId,idList));
    }

    @Override
    public List<Long> listRouteNodesIds(Long routeId) {
        return this.baseMapper.listRoteNodesIds(routeId);
    }
}
